package com.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blog.entity.SysSensitive;
import com.blog.entity.SysUser;
import com.blog.service.SysSensitiveService;

import com.blog.util.StringUtil;
import com.blog.util.dto.AjaxResult;
import com.blog.util.laydto.LayDto;
import com.blog.util.laydto.LayDtoUtil;
import com.blog.util.uuid.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 敏感词控制类
 * @author cgx
 * @Date 2022/8/16 21:46
 */
@Controller
@Slf4j
@RequestMapping("/sensitive")
public class SysSensitiveController {

    @Resource
    SysSensitiveService sensitiveService;

    /**
     * 获取敏感词表格数据
     * @param
     * @return
     */
    @GetMapping("/admin/tableList")
    @ResponseBody
    public LayDto tableList(@RequestParam(value = "sensitiveWord",required = false) String sensitiveWord,
                            @RequestParam(value = "createPeople",required = false) String createPeople){
        List<Map<String, Object>> tableList = sensitiveService.tableList(sensitiveWord,createPeople);
        return LayDtoUtil.returnSuccess(0,"",tableList.size(),tableList);
    }

    /**
     * 删除敏感词通过id
     * @param ids
     * @return
     */
    @PostMapping("/admin/deleteSensitive")
    @ResponseBody
    public AjaxResult deleteByIds(@RequestBody List<String> ids){
        sensitiveService.removeByIds(ids);
        return AjaxResult.success();
    }

    @GetMapping("/admin/sensitive-add")
    public String sensitiveAdd(){
        return "admin/article/sensitive/sensitive-add";
    }

    /**
     * 保存敏感词
     * @param
     * @return
     */
    @PostMapping("/admin/add")
    @ResponseBody
    public AjaxResult sensitiveAdd(@RequestBody SysSensitive sysSensitive){
        Subject subject = SecurityUtils.getSubject();
        SysUser sysUser = (SysUser)subject.getPrincipal();

        sysSensitive.setId(UUIDUtils.getUUId());
        sysSensitive.setCreatePeople(sysUser.getUsername());
        sysSensitive.setActive(1);

        sensitiveService.save(sysSensitive);
        return AjaxResult.success();
    }

    @GetMapping("/admin/sensitive-edit")
    public String sensitiveEdit(@RequestParam("sensitiveId") String sensitiveId,Model model){
        SysSensitive byId = sensitiveService.getById(sensitiveId);
        model.addAttribute("sensitive",byId);
        return "admin/article/sensitive/sensitive-edit";
    }

    @PostMapping("/admin/updateById")
    @ResponseBody
    public AjaxResult updateSensitive(@RequestBody SysSensitive sysSensitive){
        sensitiveService.updateById(sysSensitive);
        return AjaxResult.success(sysSensitive);
    }
}
